mui.init();

var old_back = mui.back;
mui.back = function(){
	var targetPageId = plus.storage.getItem(KeyCfg.targetPageId);	
	if(!targetPageId){
		mui.alert("参数：targetPageId 为 null ,无法跳转到下一步");
		return;
	}
	var targetPage = plus.webview.getWebviewById(targetPageId);
    mui.fire(targetPage,'reload',{});
    targetPage.show();
}

mui.plusReady(function(){
	loaddataSerucrity();
	var name = plus.storage.getItem(KeyCfg.userName);
	$("#name").val(name);
	
	var email_code = CmwMcore.$('email_code');
	MeventMgr.onTouchstart(email_code,function(event,self){
		var a= document.getElementById("a");
		a.checked=true;
		$(".emailcode").show();
		$(".qypwd").hide();
		$(".resetypwd").hide();
	});
	
	var phone_answer = CmwMcore.$('phone_answer');
	MeventMgr.onTouchstart(phone_answer,function(event,self){
		var b= document.getElementById("b");
		b.checked=true;
		$(".emailcode").hide();
		$(".qypwd").show();
		$(".resetypwd").hide();
	});
	
	
	
	/**------- 邮箱验证码 -------*/
	var btn_email =CmwMcore.$('btn_email');
	MeventMgr.onTouchstart(btn_email,function(event,self){
		if(!valid()) return;
		submitForm();
	});
	
	
	/**------- 手机验证+安全问题 -------*/
	var btn_qPcode =CmwMcore.$('btn_qPcode');
	MeventMgr.onTouchstart(btn_qPcode,function(event,self){
		if(!valid1()) return;
		submitForm1();
	});
	
	/**------- 确认邮箱-------*/
	var btn_resetemail =CmwMcore.$('btn_resetemail');
	MeventMgr.onTouchstart(btn_resetemail,function(event,self){
		if(!valid2()) return;
		submitForm2();
	});
	

    
    function submitForm(){
    	var email =$("#cnewEmail").val();
    	var randcode =$("#emailCode").val();
    	var pars ={email:email,randcode:randcode};
    	var url =UrlConfig.updateEmail;
    	MuiEventMgr.ajax(url,{pars:pars,
	  	sfn : function(data){
	  		var status = data.status;
	  		if(status==1){
     			mui.alert("邮箱修改成功");
     			mui.back();
     		}else if(status == 2){
     			mui.alert("验证码错误，请重新输入");
     		}else if(status == 3){
     			mui.alert("修改失败");
     		}else if(status == 4){
     			mui.alert("输入的邮箱帐号已存在，请重新输入");
     		}
	  	},ffn : function(err){
	  		mui.alert("获取数据失败：["+err+"]");
	  		
	  	}
	 });
    }
    
    function submitForm1(){
		var answer1 = $("#answer1").val();
		var answer2 = $("#answer2").val();
		var questionone = $("#questionone").val();
		var questiontwo = $("#questiontwo").val();
		var phone = $("#phonecode").val();
		var smstype="validPassword";
		var pars={answer1:answer1,answer2:answer2,questionone:questionone,questiontwo:questiontwo,phone:phone,smstype:smstype};
		var url =UrlConfig.validSafeAnswer;
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		var status = data.status;
		  		if (status == "true") {
					mui.alert("验证成功");
					$(".qbox").hide();
					$(".qcode").hide();
					$(".qypwd").hide();
					$(".resetypwd").show();
				} else if (status == "errorphone") {
					mui.alert("手机验证码错误");					
				} else {
					mui.alert("安全问题回答错误");					
				}
	        		
		  	},ffn : function(err){
		  		mui.alert("获取数据失败：["+err+"]");
		  	}
		});
	}
    
    function submitForm2(){
		var emailone_val = $("#emailone").val();
		
		
		var pars={emailone_val:emailone_val};
		var url =UrlConfig.confirmEmail;
		MuiEventMgr.ajax(url,{pars:pars,
		  	sfn : function(data){
		  		var status = data.status;
	  		if(status==1){
     			mui.alert("邮箱修改成功");
     			mui.back();
     		}else if(status == 3){
     			mui.alert("修改失败");
     		}else if(status == 4){
     			mui.alert("输入的邮箱帐号已存在，请重新输入");
     		}
		  	},ffn : function(err){
		  		mui.alert("获取数据失败：["+err+"]");
		  	}
		});
	}
    
    function valid(){
    	var newEmail =$("#newEmail").val();
    	var cnewEmail =$("#cnewEmail").val();
    	var emailCode =$("#emailCode").val();
    	
    	if(!emailCode){
			mui.alert("请输入验证码!");
			return false;
		}
    	if(!cnewEmail){
			mui.alert("请输入新电子邮箱!");
			return false;
		}
    	
    	if(!newEmail){
			mui.alert("请输入新电子邮箱!");
			return false;
		}
		
		var pattern =/^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$/.test(newEmail);
		if(!pattern){
			mui.alert("电子邮箱格式错误，请重新填写!");
			return false;
		}
		
		if(newEmail != cnewEmail){
			mui.alert("两次邮箱不一致!");
			return false;
		}
    	return true;
    }
    
    function valid1(){
    	var answer1_val = $("#answer1").val();
		var answer2_val =$("#answer2").val();
		var phonecode_val =$("#phonecode").val();
		if(!answer1_val || !answer1_val.trim()){
			mui.alert("问题一不能为空!");
			return false;
		}
		if(!answer2_val || !answer2_val.trim()){
			mui.alert("问题二不能为空!");
			return false;
		}
		if(!phonecode_val || !phonecode_val.trim()){
			mui.alert("验证码不能为空!");
			return false;
		}
		
		return true;
    }
    
    function valid2(){
    	var emailone_val = $("#emailone").val();
		var emailtwo_val =$("#emailtwo").val();
		if(!emailone_val || !emailone_val.trim()){
			mui.alert("新邮箱不能为空!");
			return false;
		}
		if(!emailtwo_val || !emailtwo_val.trim()){
			mui.alert("再确认邮箱不能为空!");
			return false;
		}
		var pattern =/^[_A-Za-z0-9-\+]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\.[A-Za-z0-9]+)*(\.[A-Za-z]{2,})$/.test(emailone_val);
		if(!pattern){
			mui.alert("电子邮箱格式错误，请重新填写!");
			return false;
		}
		if(emailone_val!=emailtwo_val){
			mui.alert("两次邮箱不一致!");
			return false;
		}
		
		
		return true;
    }
});


function loaddataSerucrity(){
	var url = UrlConfig.getSerucrity
	var guid = plus.storage.getItem(KeyCfg.userId);
	var pars={guid:guid};
	// 弹出系统等待对话框   
	//var w = mui.plus.nativeUI.showWaiting( "正在加载..." );
	MuiEventMgr.ajax(url,{pars:pars,
	  	sfn : function(data){
	  		var list = data.list;
	  		var answer1 = list[0];
	  		var answer2 = list[1];
	  		var question1 =answer1.problem;
	  		var question2 =answer2.problem;
	  		$("#questionone").val(question1);
	  		$("#questiontwo").val(question2);
	  		  		
	  	},ffn : function(err){
	  		mui.alert("获取数据失败：["+err+"]");
	  		//mui.plus.nativeUI.closeWaiting();
	  	}
	 });
	
}
	

